package com.luffer.springboot.demo.mqbasis.mqServer;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import java.util.concurrent.ArrayBlockingQueue;

/**
 * @Author: dengkaibing
 * @Description: 消息中心Broker
 * @Date: 2021/8/3 14:12
 * @company 翼支付
 */
@Slf4j
public class Broker {
    private final static int MAX_SIZE=3;
    /**
     * 初始化消息队列
     */
    private static ArrayBlockingQueue<String> messageQueue=new ArrayBlockingQueue<>(MAX_SIZE);

    /**
     *  生产消息
     * @param message
     */
    public static boolean produce(String message){
        if(messageQueue.offer(message)){
            log.info("成功向消息中心发送投递消息：message:{}, 当前消息数量为:{}",message,messageQueue.size());
            return true;
        }
        return false;
    }

    public static  String consumeMessage(){
        String msg=messageQueue.poll();
        if(StringUtils.isEmpty(msg)){
            log.info("消息中心无消息可以发送");
        }
        log.info("当前消费信息为：message:{},当前消息暂存数量:{}",msg,messageQueue.size());
        return msg;
    }
}
